home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 24 / Mac Magazin and MacEasy Magazine CD - Issue 24.iso / Online / Drag-n-Drop-Email-as.05 / Drag n Drop Email (text) < prev    next >
Text File  |  1996-07-17  |  8KB  |  287 lines

  1.  
  2. property isitSet : false
  3. property useSpeech : "unspecified"
  4. property whichVoice : {"Fred"}
  5. property emailHost : ""
  6. property returnAddress : ""
  7. property userName : ""
  8. property userPassword : ""
  9.  
  10. global serverResponse, sss, inBox
  11.  
  12. on run
  13.     
  14.     if not isitSet then
  15.         configure()
  16.     end if
  17.     
  18.     set closeAfter to false
  19.     if not (PPPopened) then
  20.         set closeAfter to true
  21.         openPPP (getPPPServer)
  22.         repeat while not (PPPopened)
  23.         end repeat
  24.     end if
  25.     set sss to (tcp connect to host emailHost port 110)
  26.     try
  27.         readresponse(sss)
  28.         
  29.         tcp write data "USER " & userName & return stream sss using ISO88591
  30.         readresponse(sss)
  31.         tcp write data "PASS " & userPassword & return stream sss using ISO88591
  32.         readresponse(sss)
  33.         tcp write data "STAT" & return stream sss using ISO88591
  34.         readresponse(sss)
  35.         
  36.         set oldDelimiters to AppleScript's text item delimiters
  37.         set AppleScript's text item delimiters to {" "}
  38.         set messageCount to text item 2 of serverResponse as integer
  39.         set AppleScript's text item delimiters to oldDelimiters
  40.         if messageCount ≠ 1 then
  41.             if messageCount = 0 then
  42.                 if useSpeech = "yes" then
  43.                     speak "There are no messages" voice whichVoice
  44.                 end if
  45.                 set dialogResponse to (display dialog "There are no messages for " & userName & "." buttons {"OK"} default button "OK")
  46.             else
  47.                 if useSpeech = "yes" then
  48.                     speak "There are " & messageCount & " new messages" voice whichVoice
  49.                 end if
  50.                 set dialogResponse to (display dialog "There are  " & messageCount & "  e-mail messages for " & userName & "." buttons {"OK", "Get Mail"} default button "Get Mail")
  51.             end if
  52.         else
  53.             if useSpeech = "yes" then
  54.                 speak "There is one new message" voice whichVoice
  55.             end if
  56.             set dialogResponse to (display dialog "There is  " & messageCount & "  e-mail message for " & userName & "." buttons {"OK", "Get Mail"} default button "Get Mail")
  57.         end if
  58.         if (button returned of dialogResponse = "Get Mail") then
  59.             set currentMessage to 1
  60.             
  61.             tell application "Finder"
  62.                 if not (folder "AppleScript Inbox" of desktop exists) then
  63.                     set inBox to (make folder with properties {name:"AppleScript Inbox"}) as string
  64.                 else
  65.                     set inBox to (folder "AppleScript Inbox" of desktop) as string
  66.                 end if
  67.             end tell
  68.             
  69.             repeat while currentMessage ≤ messageCount
  70.                 
  71.                 getMail(currentMessage)
  72.                 
  73.                 set currentMessage to currentMessage + 1
  74.             end repeat
  75.         end if
  76.         
  77.         tcp close stream sss
  78.         
  79.     on error msg number num
  80.         tcp close stream sss
  81.         display dialog "Script Aborted." buttons {"OK"} default button "OK"
  82.         set isitSet to false
  83.         quit
  84.     end try
  85.     
  86.     if closeAfter = true then
  87.         if (PPPopened) = true then
  88.             closePPP with hard close
  89.         end if
  90.     end if
  91.     
  92. end run
  93.  
  94. on open (docList)
  95.     set messageCount to (count of docList) as string
  96.     
  97.     if not isitSet then
  98.         configure()
  99.     end if
  100.     
  101.     set closeAfter to false
  102.     if not (PPPopened) then
  103.         set closeAfter to true
  104.         openPPP (getPPPServer)
  105.         repeat while not (PPPopened)
  106.         end repeat
  107.     end if
  108.     
  109.     set LF to ASCII character (10)
  110.     set CR to return
  111.     set CRLF to CR & LF
  112.     
  113.     set sss to (tcp connect to host emailHost port 25)
  114.     readresponse(sss)
  115.     tcp write data "HELO " & (tcp my address) & return stream sss using ISO88591
  116.     readresponse(sss)
  117.     try
  118.         
  119.         repeat with aFile in docList
  120.             set aFile to contents of aFile
  121.             tcp write data "mail from: " & userName & return ¬
  122.                 stream sss using ISO88591
  123.             readresponse(sss)
  124.             
  125.             set afilename to aFile as string
  126.             set oldDelimiters to AppleScript's text item delimiters
  127.             set AppleScript's text item delimiters to {":"}
  128.             set theFileName to last text item of afilename
  129.             set AppleScript's text item delimiters to oldDelimiters
  130.             set emailSubject to theFileName as text
  131.             open for access afilename
  132.             set sendToaddress to (read afilename before return)
  133.             set mailContents to (read afilename)
  134.             close access afilename
  135.             
  136.             tcp write data "rcpt to: " & sendToaddress & return ¬
  137.                 stream sss using ISO88591
  138.             readresponse(sss)
  139.             tcp write data "data" & return stream sss using ISO88591
  140.             readresponse(sss)
  141.             tcp write data "To: " & sendToaddress & return stream sss using ISO88591
  142.             tcp write data "Subject: " & emailSubject & return stream sss using ISO88591
  143.             tcp write data mailContents & return stream sss using ISO88591
  144.             tcp write data "." & return stream sss using ISO88591
  145.             readresponse(sss)
  146.         end repeat
  147.         
  148.         if useSpeech = "yes" then
  149.             if messageCount = "1" then
  150.                 speak messageCount & " message has been sent" voice whichVoice
  151.             else
  152.                 speak messageCount & " messages have been sent" voice whichVoice
  153.             end if
  154.         end if
  155.         
  156.         tcp close stream sss
  157.         
  158.         if closeAfter = true then
  159.             if (PPPopened) = true then
  160.                 closePPP with hard close
  161.             end if
  162.         end if
  163.         return
  164.     on error msg number num
  165.         tcp close stream sss
  166.         display dialog "Script Aborted." buttons {"OK"} default button "OK"
  167.         set isitSet to false
  168.         quit
  169.     end try
  170.     
  171. end open
  172.  
  173. on getMail(currentMessage)
  174.     set LF to ASCII character (10)
  175.     set nowTotal to (time string of (current date))
  176.     set oldDelimiters to AppleScript's text item delimiters
  177.     set AppleScript's text item delimiters to {":"}
  178.     set nowHour to text item 1 of nowTotal
  179.     set nowMin to text item 2 of nowTotal
  180.     set nowSec to text item 3 of nowTotal
  181.     set nowString to nowHour & "h" & nowMin & "m" & nowSec & "s"
  182.     set AppleScript's text item delimiters to oldDelimiters
  183.     set currentFilename to inBox & "Message #" & currentMessage & " " & nowString
  184.     open for access file currentFilename with write permission
  185.     tcp write data "RETR " & currentMessage & return stream sss using ISO88591
  186.     readresponse(sss)
  187.     set oldDelimiters to AppleScript's text item delimiters
  188.     set AppleScript's text item delimiters to {" "}
  189.     set messageSize to text item 2 of serverResponse as integer
  190.     set AppleScript's text item delimiters to oldDelimiters
  191.     
  192.     repeat until (bytes waiting of (tcp status stream sss) = 0)
  193.         
  194.         write (tcp read stream sss using ISO88591) to file currentFilename starting at eof
  195.         
  196.         set beginWait to time of (current date)
  197.         repeat until time of (current date) > beginWait + 1
  198.         end repeat
  199.     end repeat
  200.     
  201.     close access file currentFilename
  202.     
  203.     tcp write data "DELE " & currentMessage & return stream sss using ISO88591
  204.     readresponse(sss)
  205. end getMail
  206.  
  207.  
  208. on readresponse(sstream)
  209.     set LF to ASCII character (10)
  210.     
  211.     repeat until (tcp ahead characters LF stream sstream)
  212.     end repeat
  213.     set serverResponse to (tcp read until characters LF stream sstream using ISO88591)
  214.     
  215. end readresponse
  216.  
  217. on configure()
  218.     set defaultInfo to {"user:pAssWoRD@mail.wherever.com"}
  219.     
  220.     checkOSAX({"TCP/IP Scripting Addition", "MacPPP Control"})
  221.     if result = false then
  222.         display dialog "Certain essential Scripting Additions are missing." & return ¬
  223.             & "See the enclosed ReadMe for more information." buttons {"OK"} default button "OK"
  224.         quit
  225.     end if
  226.     set dialogResponse to (display dialog "Enter a username, password, and mail server:" default answer defaultInfo)
  227.     if (button returned of dialogResponse ≠ "OK") then
  228.         display dialog "Script Aborted." buttons {"OK"} default button "OK"
  229.         quit
  230.     else
  231.         set userInfo to text returned of dialogResponse
  232.         if userInfo = defaultInfo then
  233.             display dialog "Script Aborted." buttons {"OK"} default button "OK"
  234.             quit
  235.         end if
  236.     end if
  237.     
  238.     set oldDelims to text item delimiters of AppleScript
  239.     set text item delimiters of AppleScript to {":"}
  240.     set userName to first text item of userInfo
  241.     set intermediateStep to last text item of userInfo
  242.     
  243.     set text item delimiters of AppleScript to {"@"}
  244.     set userPassword to first text item of intermediateStep
  245.     set returnAddress to last text item of intermediateStep
  246.     set emailHost to last text item of intermediateStep
  247.     set text item delimiters of AppleScript to oldDelims
  248.     
  249.     if useSpeech = "unspecified" then
  250.         checkOSAX({"Speech"})
  251.         if result = false then
  252.             set useSpeech to "no"
  253.         else
  254.             set useSpeech to "yes"
  255.             if "Fred" is not in (list voices) then
  256.                 set whichVoice to "Bruce"
  257.                 if "Bruce" is not in (list voices) then
  258.                     set useSpeech to "no"
  259.                 end if
  260.             end if
  261.         end if
  262.     end if
  263.     set isitSet to true
  264. end configure
  265.  
  266. on checkOSAX(OSAXTest)
  267.     tell application "Finder"
  268.         set OSAXList to name of every item of folder "Scripting Additions" of extensions folder
  269.         set failedTest to ""
  270.         repeat with oneItem in OSAXTest
  271.             if oneItem is not in OSAXList then
  272.                 set failedTest to failedTest & tab & oneItem & return
  273.             end if
  274.         end repeat
  275.         if failedTest is "" then
  276.             return true
  277.         else
  278.             return false
  279.         end if
  280.     end tell
  281. end checkOSAX
  282.  
  283. on quit
  284.     continue quit
  285. end quit
  286.  
  287.